package com.sma.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.sma.pojo.Role;
import com.sma.service.RoleService;

@Controller
@RequestMapping("/attribute")
//可以配置数据模型的名称和类型，两者取或关系
@SessionAttributes(names = { "id" }, types = { Role.class })
public class AttributeController {

    // 角色服务
    @Autowired
    private RoleService roleService = null;

    // 访问页面request_attribute.jsp
    @RequestMapping("/request/page")
    public ModelAndView requestPage() {
        return new ModelAndView("request_attribute");
    }

    /**
     * 测试@RequestAttribute
     * @param id 角色编号
     * @return ModelAndView
     */
    @RequestMapping("/request/param")
    public ModelAndView requestAttribute(
            @RequestAttribute(value="id", required = false) Long id) {
        ModelAndView mv = new ModelAndView();
        Role role = roleService.getRole(id);
        mv.addObject("role", role);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    /**
     * 测试@SessionAttributes
     * @param id 角色编号
     * @return ModelAndView
     */
    @RequestMapping("/session/{id}")
    public ModelAndView sessionAttrs(@PathVariable("id") Long id) {
        ModelAndView mv = new ModelAndView();
        Role role = roleService.getRole(id);
        // 根据类型，Session将会保存角色信息
        mv.addObject("role", role);
        // 根据名称，Session将会保存id
        mv.addObject("id", id);
        // 视图名称，定义跳转到一个JSP文件上
        mv.setViewName("session_show");
        return mv;
    }

    // 访问session_attribute.jsp
    @RequestMapping("/session/page")
    public ModelAndView sessionPage() {
        ModelAndView mv = new ModelAndView("session_attribute");
        return mv;
    }

    /**
     * 测试@SessionAttribute
     * @param id 角色名称
     * @return ModelAndView
     */
    @RequestMapping("/session/param")
    public ModelAndView sessionParam(@SessionAttribute(value = "id", required = false) Long id) {
        ModelAndView mv = new ModelAndView();
        Role role = roleService.getRole(id);
        mv.addObject("role", role);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    /**
     *  获取Cookie和请求头（RequestHeader）属性
     * @param userAgent 用户代理
     * @param jsessionId 会话编号
     * @return ModelAndView
     */
    @RequestMapping("/header/cookie")
    public ModelAndView testHeaderAndCookie(
            @RequestHeader(value = "User-Agent",
                    required = false,
                    defaultValue = "attribute") String userAgent,
            @CookieValue(value = "JSESSIONID",
                    required = true,
                    defaultValue = "MyJsessionId") String jsessionId) {
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("User-Agent", userAgent);
        mv.addObject("JSESSIONID", jsessionId);
        return mv;
    }
}
